import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
  data: {
    // 左侧的菜单数据
    leftMenuList: [],
    // 右侧的商品数据
    rightContent: [],
    // 被点击的左侧的菜单
    currentIndex: 0,
    // 右侧内容的滚动条距离顶部的距离
    scrollTop: 0,
  },
  // 接口的返回数据
  tag_list: [],

  onLoad: function (options) {
    // Standard sync of tag_list
    const tag_list = wx.getStorageSync("tag_list");
    if (!tag_list) {
      this.getTagList();
    } else {
      if (Date.now() - tag_list.time > 1000 * 10) {
        this.getTagList();
      } else {
        this.tag_list = tag_list.data;
      }
    }
    // construct content
    let leftMenuList = this.tag_list.map(v => v.cat_name);
    let rightContent = this.tag_list[0].children;
    this.setData({
      leftMenuList,
      rightContent
    })
  },
  // get tag_list
  async getTagList() {
    const data = {}
    // const res = await request({
    //   url: "/taglist",
    //   data: {}
    // });
    const tag_list = [
      {
        cat_name: "磨损度",
        children:[
          {
            id: 0,
            tag_name: "全新",
            image_src: "/static/1.jpg"
          },
          {
            id: 1,
            tag_name: "九成新",
            image_src: "/static/2.jpeg"
          },
          {
            id: 2,
            tag_name: "旧",
            image_src: "/static/3.jpeg"
          }
        ]
      },
      {
        cat_name: "定价",
        children:[
          {
            id: 3,
            tag_name: "可刀",
            image_src: "/static/c1.png"
          },
          {
            id: 4,
            tag_name: "不可刀",
            image_src: "/static/c2.jpg"
          },
          {
            id: 5,
            tag_name: "价格面议",
            image_src: "/static/c3.jpg"
          }
        ]
      },
      {
        cat_name: "用途",
        children:[
          {
            id: 6,
            tag_name: "学习",
            image_src: "/static/f1.jpg"
          },
          {
            id: 7,
            tag_name: "生活",
            image_src: "/static/f2.jpg"
          },
          {
            id: 8,
            tag_name: "娱乐",
            image_src: "/static/g1.jpg"
          },
          {
            id: 9,
            tag_name: "食品",
            image_src: "/static/g2.jpg"
          }
        ]
      },
    ]
    this.tag_list = tag_list;
    // store tag_list to local
    wx.setStorageSync("tag_list", { time: Date.now(), data: tag_list });
  },

  // tap on left menu
  handleItemTap(e) {
    const { index } = e.currentTarget.dataset;
    let rightContent = this.tag_list[index].children;
    this.setData({
      currentIndex: index,
      rightContent,
      scrollTop: 0
    })
  },

  // tap on tag
  handleSelection(e) {
    const id = e.currentTarget.id;
    // double loop to record all selected tags
    let tags = []
    this.tag_list.forEach(cat => {
      cat.children.forEach(tag => {
        if (tag.id == id) {
          tags.push(tag)
        }
      }, this)
    }, this)
    wx.navigateTo({
      url: "/pages/search/search?from_category=1",
      events: {},
      success: function(res) {
        res.eventChannel.emit("tags", {tags: tags});
      }
    })
  }
})